package com.ftg.product.shangchuan;

//import com.ftg.learn.smhtml.pojo.Music;
//import com.ftg.learn.smhtml.pojo.Singer;
//import com.ftg.learn.smhtml.service.MusicService;
//import com.ftg.learn.smhtml.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class SingerController {


//    @Autowired
//    SingerService deptService;
//
//    @GetMapping("/showsinger")
//    public Map<String,Object> showSinger(String id, int page, int limit){
//        return deptService.showSinger(id,page,limit);
//    }


    @PostMapping("/upload")
    public Map<String,Object> shang(@RequestBody MultipartFile file, HttpServletRequest request) {
        System.out.println(file.getOriginalFilename());
        String desFilePath = "";
        String oriName ="";
        Map<String, String> dataMap = new HashMap<>();
        Map<String, Object> dea = new HashMap<>();
        try {
            // 1.获取原文件名
            oriName = file.getOriginalFilename();
            // 2.获取原文件图片后缀，以最后的.作为截取(.jpg)
            String extName = oriName.substring(oriName.lastIndexOf("."));
            // 3.生成自定义的新文件名，这里以UUID.jpg|png|xxx作为格式（可选操作，也可以不自定义新文件名）
            String uuid = UUID.randomUUID().toString();
            String newName = uuid + extName;
            // 4.获取要保存的路径文件夹
            String realPath = "D://images";
            // 5.保存图片
            desFilePath = realPath + "/" + newName;
            File desFile = new File(desFilePath);
            file.transferTo(desFile);
            System.out.println(desFilePath);
            // 6.返回保存结果信息
            dea.put("code","0");
            dea.put("data",newName);
            dea.put("msg","成功！");
            return dea;
        } catch (IllegalStateException e) {
            dea.put("code","1");
            dea.put("msg","失败！");
            System.out.println(desFilePath + "图片保存失败");
            return dea;
        } catch (IOException e) {
            dea.put("code","1");
            dea.put("msg","失败！");
            System.out.println(desFilePath + "图片保存失败--IO异常");
            return dea;
        }
    }

//    @DeleteMapping("/delSinger/{id}")
//    public int delMusic(@PathVariable String[] id){
//        return deptService.delSinger(id);
//    }
//
//    @DeleteMapping("/delphoto/{id}")
//    public int delppp(@PathVariable String id ){
//        String photo = deptService.selone(id).get(0).getSinger_photo();
//        File file1 = new File("D://abc/"+photo);
//        file1.delete();
//        return 1;
//    }
//
//    @GetMapping("/showSinger")
//    public List<Singer> showSinger1(){
//        return deptService.showSinger1();
//    }
//
//    @PostMapping("/addSinger")
//    public int addMusic(Singer data){
//        System.out.print(data.getSinger_photo());
//        return deptService.addSinger(data);
//
//    }
//
//
//    @PostMapping("/updSinger")
//    public int updMusic(Singer data){
//        return deptService.updSinger(data);
//
//    }
}
